package com.example.knowledge.mvvm.fragment

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.example.knowledge.R
import com.example.knowledge.mvvm.adapter.ReportListAdapter
import com.example.knowledge.mvvm.bean.RecordsBean
import com.example.knowledge.mvvm.viewmodel.ReportViewModel
import com.gsafety.libraryktx.base.BaseFragment
import com.gsafety.libraryktx.bean.LoadState
import com.gsafety.libraryktx.config.Constant
import com.gsafety.libraryktx.expand.create
import com.gsafety.libraryktx.expand.toast
import com.gsafety.libraryktx.util.RecyclerViewHelper
import com.gsafety.libraryktx.widget.CommonRefreshView
import kotlinx.android.synthetic.main.know_report_list_activity.*

/**
 * 报告列表
 */
class ReportListFragment : BaseFragment(), CommonRefreshView.RefreshListener {
    var datas = ArrayList<RecordsBean>()
    var mAdapter: ReportListAdapter? = null
    var current: Int = 0
    var mEtSearch: EditText? = null
    var code:String?=null
    var type:String?=null
    private lateinit var mv: ReportViewModel
    override fun initView(view: View?) {
        mEtSearch = view?.findViewById(R.id.et_search)
        mEtSearch?.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                crv_common_refresh.autoRefresh()
            }
            false
        }
        crv_common_refresh.setRrefreshListener(this)
        initmodel()
    }

    override fun initData() {
        code = arguments?.getString(Constant.Extra_CODE_KEY)
        type = arguments?.getString(Constant.Extra_TYPE_KEY)
        var rv_msg = view?.findViewById<RecyclerView>(R.id.rv_msg)
        mAdapter = ReportListAdapter(datas)
        RecyclerViewHelper.initRecyclerViewV(mContext, rv_msg!!, false, mAdapter)
        crv_common_refresh.autoRefresh()
    }

    override fun getLayoutId(): Int {
        return R.layout.know_report_list_activity
    }


    private fun initmodel() {
        mv = ReportViewModel()
        mv = create(ReportViewModel::class)
        mv.mReportRiskValue.observe(this, Observer {
            if (it != null) {
                current = it.current.toInt()
                if (crv_common_refresh.isRefreshing()) {
                    mAdapter?.setNewData(it.records)
                } else {
                    mAdapter?.let { adapter ->
                        var mDatas = adapter.data
                        mDatas.addAll(it.records)
                        mAdapter?.setNewData(mDatas)
                    }

                }
                if(it.records.size<10){
                    crv_common_refresh.setEnableLoadMore(false)
                }
            }
            crv_common_refresh.finishRefresh()
        })
        mv.loadState.observe(this, Observer {
            when (it) {
                is LoadState.Fail -> {
                    mContext?.toast(it.msg)
                    crv_common_refresh.finishRefresh()
                }
            }
        })

    }

    companion object {
        fun newInstance(type: String, code: String): ReportListFragment {
            var fragment = ReportListFragment()
            var args = Bundle()
            args.putString(Constant.Extra_TYPE_KEY, type)
            args.putString(Constant.Extra_CODE_KEY, code)
            fragment.arguments = args
            return fragment
        }
    }

    override fun onRefresh() {
        mv.requestriskReport(mEtSearch?.text.toString(), type!!, code!!, 1)
    }

    override fun onLoadMore() {
        mv.requestriskReport(mEtSearch?.text.toString(), type!!, code!!, current+1)
    }
}